﻿<? 
	error_reporting(E_ERROR);
	session_start();
	
	header ('Content-type: text/html; charset=utf-8');   
	require_once "../db/connection.inc"; 
	include_once "JSON.php";
	$action = $_REQUEST["action"];
	
	$json = new Services_JSON();
	 
	if("$action"=="list"){
		$start = $_REQUEST["start"] ;
		$end = $_REQUEST["limit"]  ;
		if("$start"=="") $start=0;
		if("$end"=="") $end=20;
		
		$sort = $_REQUEST["sort"];
		$direction = $_REQUEST["dir"];
		
		if($_REQUEST["vc__comuni/Nome"]!="")  $_REQUEST["vc__comuni/Nome"] = '*'.$_REQUEST["vc__comuni/Nome"].'*';
		if($_REQUEST["vc__strade/Nome"]!="")  $_REQUEST["vc__strade/Nome"] = '*'.$_REQUEST["vc__strade/Nome"].'*';
		if($_REQUEST["vc__Localita"]!="")  $_REQUEST["vc__Localita"] = '*'.$_REQUEST["vc__Localita"].'*';
		 
		if("$sort"=="") {
			$sort = 'Localita';
			$direction='';
		}	
			
			
		$sql = "
			SELECT opere.*, 	
			comuni.nome as DescComune,
			comuni.Siglaprovincia as Siglaprovincia,
			strade.Tipo , 
			strade.Numero, 
			strade.Estensione, 
			strade.nome  as DescStrada,
			concat_ws(' ',strade.Tipo,strade.Numero,strade.Estensione,strade.Nome) As DesEstesaStrada,
			concat_ws('',comuni.Nome,' (' , comuni.Siglaprovincia ,')' ) As DesEstesaComune
			FROM opere	
			left join comuni on comuni.id=opere.Idcomune	
			left join strade on strade.id=opere.Idstrada	
			ORDER BY $sort $direction
            LIMIT $start,$end
			 
		" ; 
		 
		$dt =rs($sql,$_REQUEST);
		
		$sqlCount = "
			SELECT count(*) As total 
            FROM opere 
		 	left join comuni on comuni.id=opere.Idcomune	
			left join strade on strade.id=opere.Idstrada	
			
		" ;
		
		$totalFromSession = $_SESSION["OPERE_TOTAL"];
		
		if($start==0 || !isset($totalFromSession) ){
			$dtc =rs($sqlCount,$_REQUEST);
			$values = $dtc->getRow();
			$totalFromSession = $values["total"] ;
			$_SESSION["OPERE_TOTAL"]=$totalFromSession;
		} 
		 
		exit( recordsetToJson($dt,$totalFromSession,$sql)  );
		
	} else if("$action"=="save"){
		
		$postedData = str_replace( '##','"',$_REQUEST['data']); 
		
		$data = $json->decode($postedData); 
		$db = db();	
		 
		$record = $data[0]; 
		 
		$Dtrilievo = $db->quoteSmart($record->Dtrilievo)  ;
		$Localita = $db->quoteSmart($record->Localita)  ;
		$Note = $db->quoteSmart($record->Note)  ;
		$Idstrada ="$record->Idstrada"==""?"null":$record->Idstrada  ;
		$Idcomune ="$record->Idcomune"==""?"null":$record->Idcomune  ; 
		$Km ="$record->Km"==""?0:$record->Km  ; 
		$Metri ="$record->Metri"==""?0:$record->Metri  ; 
		$Longitudine ="$record->Longitudine"==""?0:$record->Longitudine  ; 
		$Latitudine ="$record->Latitudine"==""?0:$record->Latitudine  ; 
		$Distanzasedestradale ="$record->Distanzasedestradale"==""?0:$record->Distanzasedestradale  ; 
		$Quotaslm ="$record->Quotaslm"==""?0:$record->Quotaslm  ; 
		
		if( $record->Id >0 ) {
			$sql = " 
				UPDATE opere  
				SET   
					Dtrilievo=$Dtrilievo ,
					Localita=$Localita ,
					Note=$Note ,
					Idstrada=$Idstrada ,
					Idcomune=$Idcomune ,
					Km=$Km ,
					Metri=$Metri ,
					Longitudine=$Longitudine ,
					Latitudine=$Latitudine ,
					Distanzasedestradale=$Distanzasedestradale , 
					Quotaslm=$Quotaslm 
				where 
					Id=$record->Id" ;			
		}else{
		
		}
		
		$res = $db->query($sql); 
		
		//echo $sql;
		//print_r($res);
		if(!$res) exit("'Dati non memorizzati a causa di un errore'")	;	 
		else exit("true");
	}
		
	echo "false";	
	
?>